package com.lc.stock.config;

import com.alibaba.fastjson.JSONArray;
import com.lc.stock.common.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author lichen
 * @description Spring 配置的缓存
 * @menu
 * @date 2023/2/6 16:42
 */
@Configuration
public class SpringCachingConfig {

    @Value("${cache_stock}")
    private String cache_stock;
    @Autowired
    private LicenseProperties licenseProperties;

    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        Set<ConcurrentMapCache> mapCaches = new HashSet<>();
        //配置cache类型与目录
        mapCaches.add(new ConcurrentMapCache(cache_stock));
        cacheManager.setCaches(mapCaches);
        cacheManager.afterPropertiesSet();

        //初始化lincese的缓存
        List<String> licences = licenseProperties.getLicences();
        Cache cache = cacheManager.getCache(cache_stock);
        String str = JSONArray.toJSONString(licences);
        cache.put(Constant.licence_key,str);
        return cacheManager;
    }
}
